﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace SPMS.Utility
{
    /// <summary>
    /// 集合处理辅助类
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]   2008-9-23 4:35 PM    Created
    /// </history>
    public sealed class CollectionUtility
    {
        #region ---IsNullOrEmpty

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <typeparam name="T">数组元素的类型</typeparam>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:35 PM    Created
        /// </history>
        public static bool IsNullOrEmpty<T>( T[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( char[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( int[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( float[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( long[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( double[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( byte[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( short[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的数组是否为Null或者为空数组
        /// </summary>
        /// <param name="value">需要进行检查的数组</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( decimal[] value )
        {
            return (null == value || 0 == value.Length);
        }

        /// <summary>
        /// 判断指定的集合是否为Null或者为空集合
        /// </summary>
        /// <param name="value">需要进行检查的集合</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty( System.Collections.ICollection value )
        {
            return (null == value || 0 == value.Count);
        }

        /// <summary>
        /// 判断指定的集合是否为Null或者为空集合
        /// </summary>
        /// <param name="value">需要进行检查的集合</param>
        /// <returns>
        /// 	<c>true</c>：数组为Null或者为空数组<br/>
        /// 	<c>false</c>：数组不为Null或者至少包含一个元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:40 PM    Created
        /// </history>
        public static bool IsNullOrEmpty<T>( IEnumerable<T> value )
        {
            return (null == value || 0 == value.Count());
        }

        #endregion

        #region ---IsBlank

        /// <summary>
        /// 判断指定的数组中的元素是否包含非空元素
        /// </summary>
        /// <typeparam name="T">数组类型参数</typeparam>
        /// <param name="value">需要进行检查的目标数组</param>
        /// <returns>
        /// 	<c>true</c>：目标数组包含非空元素<br/>
        /// 	<c>false</c>：目标数组不包含非空元素
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-20 14:55    Created
        /// </history>
        public static bool HasNoNull<T>( T[] value ) where T : class
        {
            //判断数组是否为空
            if (null == value || 0 == value.Length)
            {
                return false;
            }

            //判断是否包含非空元素
            return value.Any( ( item ) =>
            {
                return null != item;
            } );
        }

        /// <summary>
        /// 判断指定的数组中的元素是否包含Null
        /// </summary>
        /// <typeparam name="T">数组类型参数</typeparam>
        /// <param name="value">需要进行检查的目标数组</param>
        /// <returns>
        /// 	<c>true</c>：目标数组包含Null<br/>
        /// 	<c>false</c>：目标数组不包含Null
        /// </returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-20 14:55    Created
        /// </history>
        public static bool HasNull<T>( T[] value ) where T : class
        {
            //判断数组是否为空
            if (null == value || 0 == value.Length)
            {
                return false;
            }

            //判断是否包含非空元素
            return value.Any( ( item ) =>
            {
                return null == item;
            } );
        }

        #endregion

        #region ---Trim

        /// <summary>
        /// 清除指定数组中所有为Null的数组元素
        /// </summary>
        /// <typeparam name="T">数组类型参数</typeparam>
        /// <param name="value">需要Trim的目标数组</param>
        /// <returns>包含目标数组中所有非空元素的新数组</returns>
        /// <remarks>如果源数组为Null，则返回空数组</remarks>
        /// <history>
        /// [zanezeng]   2008-11-20 15:01    Created
        /// </history>
        public static T[] TrimNull<T>( T[] value ) where T : class
        {
            //判断数组是否为NULL或者为空数组
            if (IsNullOrEmpty<T>( value ))
            {
                //如果为Null或者为空数组，则返回空数组
                return new T[0];
            }

            //创建非空查询
            var queryNotNull = from item in value
                               where null != item
                               select item;

            //返回数组
            return queryNotNull.ToArray();
        }

        #endregion

        #region ---Array

        /// <summary>
        /// 获得安全数组
        /// </summary>
        /// <typeparam name="T">数组类型参数</typeparam>
        /// <param name="source">源数组</param>
        /// <returns>安全数组</returns>
        /// <remarks>如果源数组为Null，则返回空数组，否则返回源数组</remarks>
        /// <history>
        /// [zanezeng]   2009-1-8 10:29    Created
        /// </history>
        public static T[] GetSafeArray<T>( T[] source )
        {
            //判断数组是否为Null
            if (null == source)
            {
                //返回空数组
                return new T[0];
            }

            //返回源数组
            return source;
        }

        /// <summary>
        /// 将指定的集合转化为数组
        /// </summary>
        /// <param name="source">需要转化的集合</param>
        /// <returns>指定参数类型、包含集合内容的数组<br/>如果源集合为Null，返回空数组</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:50 PM    Created
        /// </history>
        public static object[] ToArray( ICollection source )
        {
            //判断是否为空
            if (IsNullOrEmpty( source ))
            {
                //返回
                return new object[0];
            }

            //创建返回值
            object[] result = new object[source.Count];

            //拷贝数据
            source.CopyTo( result, 0 );

            //返回
            return result;
        }

        /// <summary>
        /// 将指定的集合转化为数组
        /// </summary>
        /// <param name="source">需要转化的集合</param>
        /// <param name="elementType">数组元素的类型</param>
        /// <returns>指定参数类型、包含集合内容的数组<br/>如果源集合为Null，返回空数组</returns>
        /// <remarks></remarks>
        /// <exception cref="System.ArgumentNullException"><paramref name="elementType"/>为Null</exception>
        /// <history>
        /// [zanezeng]   2008-9-25 2:50 PM    Created
        /// </history>
        public static Array ToArray( ICollection source, Type elementType )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( elementType, "elementType" );

            //判断是否为空
            if (IsNullOrEmpty( source ))
            {
                //返回
                return new object[0];
            }

            //创建数组
            Array result = Array.CreateInstance( elementType, source.Count );

            //用于计数的索引序号
            int index = 0;

            //循环添加数据
            foreach (object value in source)
            {
                //添加到数组中
                result.SetValue( ObjectUtility.ConvertTo( value, elementType ), index );

                //增加索引序号
                index++;
            }

            //返回
            return result;
        }

        /// <summary>
        /// 将指定的集合转化为数组
        /// </summary>
        /// <typeparam name="TTarget">目标数组元素类型参数</typeparam>
        /// <param name="source">需要转化的集合</param>
        /// <returns>指定参数类型、包含集合内容的数组<br/>如果源集合为Null，返回空数组</returns>
        /// <remarks></remarks>
        /// <exception cref="System.ArgumentNullException"><paramref name="elementType"/>为Null</exception>
        /// <history>
        /// [zanezeng]   2008-9-25 2:50 PM    Created
        /// </history>
        public static TTarget[] ToArray<TTarget>( ICollection source )
        {
            //判断是否为空
            if (IsNullOrEmpty( source ))
            {
                //返回
                return new TTarget[0];
            }

            //创建数组
            TTarget[] result = new TTarget[source.Count];

            //用于计数的索引序号
            int index = 0;

            //循环添加数据
            foreach (object value in source)
            {
                //添加到数组中
                result[index] = ObjectUtility.ConvertTo<TTarget>( value );

                //增加索引序号
                index++;
            }

            //返回
            return result;
        }

        /// <summary>
        /// 将指定的集合转化为数组
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="source">需要转化的集合</param>
        /// <returns>指定参数类型、包含集合内容的数组<br/>如果源集合为Null，返回空数组</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:50 PM    Created
        /// </history>
        public static T[] ToArray<T>( ICollection<T> source )
        {
            //判断是否为空
            if (IsNullOrEmpty( source ))
            {
                //返回
                return new T[0];
            }

            //返回
            return source.ToArray<T>();
        }

        /// <summary>
        /// 将指定的集合转化为数组
        /// </summary>
        /// <typeparam name="TSource">源集合元素类型参数</typeparam>
        /// <typeparam name="TTarget">目标数组元素类型参数</typeparam>
        /// <param name="source">需要转化的集合</param>
        /// <returns>指定参数类型、包含集合内容的数组<br/>如果源集合为Null，返回空数组</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 2:50 PM    Created
        /// </history>
        public static TTarget[] ToArray<TSource, TTarget>( ICollection<TSource> source )
        {
            //判断是否为空
            if (IsNullOrEmpty( source ))
            {
                //返回
                return new TTarget[0];
            }

            //创建数组
            TTarget[] result = new TTarget[source.Count];

            //用于计数的索引序号
            int index = 0;

            //循环添加数据
            foreach (object value in source)
            {
                //添加到数组中
                result[index] = ObjectUtility.ConvertTo<TTarget>( value );

                //增加索引序号
                index++;
            }

            //返回
            return result;
        }

        /// <summary>
        /// 将源数组添加到目标数组的最后
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="target">源数组</param>
        /// <param name="source">目标数组</param>
        /// <returns>合并之后包含源数组和目标数组内容的新数组</returns>
        /// <remarks>如果源数组或者目标数组为空，那么将返回仅包含目标数组或者源数组内容的新数组</remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 3:03 PM    Created
        /// </history>
        public static T[] AddToArray<T>( T[] target, T[] source )
        {
            //计算总的长度
            int length = 0;

            //记录索引
            int index = 0;

            //判断目标数组是否为空
            if (IsNullOrEmpty( target ) && IsNullOrEmpty( source ))
            {
                //返回空数组
                return new T[0];
            }
            //如果目标数组为空，则返回源数组
            else if (IsNullOrEmpty( target ))
            {
                return source.ToArray();
            }
            //如果源数组为空，则返回目标数组
            else if (IsNullOrEmpty( source ))
            {
                return target.ToArray();
            }

            //添加
            if (null != target && target.Length > 0)
            {
                //计算长度
                length += target.Length;

                //计算索引
                index = target.Length;
            }

            //添加
            if (null != source && source.Length > 0)
            {
                //计算长度
                length += source.Length;
            }

            //创建临时数组
            T[] result = new T[length];

            //添加
            if (null != target && target.Length > 0)
            {
                target.CopyTo( result, 0 );
            }

            //添加
            if (null != source && source.Length > 0)
            {
                source.CopyTo( result, index );
            }

            //返回
            return result;
        }

        /// <summary>
        /// 将指定的值添加到目标数组的最后
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="target">源数组</param>
        /// <param name="value">需要添加的值</param>
        /// <returns>合并之后包含源数组和目标数组内容的新数组</returns>
        /// <exception cref="System.ArgumentNullException">目标数组为Null</exception>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 3:03 PM    Created
        /// </history>
        public static T[] AddToArray<T>( T[] target, T value )
        {
            //判断目标数组是否为空
            if (IsNullOrEmpty( target ))
            {
                //返回
                return new T[] { value };
            }

            //计算总的长度
            int length = 1;

            //记录索引
            int index = 0;

            //添加
            if (null != target && target.Length > 0)
            {
                //计算长度
                length += target.Length;

                //计算索引
                index = target.Length;
            }

            //创建临时数组
            T[] result = new T[length];

            //添加
            if (null != target && target.Length > 0)
            {
                target.CopyTo( result, 0 );
            }

            //添加
            result[length - 1] = value;

            //返回
            return result;
        }

        /// <summary>
        /// 将源数组指定位置起的元素添加到目标数组
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="target">源数组</param>
        /// <param name="source">目标数组</param>
        /// <param name="startIndex">源数组的起始位置</param>
        /// <returns>合并之后包含源数组和目标数组内容的新数组</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="source"/>为Null</exception>
        /// <exception cref="System.IndexOutOfRangeException"><paramref name="startIndex"/>为-1或者超出target数组的范围</exception>
        /// <remarks>如果源数组或者目标数组为空，那么将返回仅包含目标数组或者源数组内容的新数组</remarks>
        /// <history>
        /// [zanezeng]   2008-9-25 3:03 PM    Created
        /// </history>
        public static T[] AddToArray<T>( T[] target, T[] source, int startIndex )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( source, "source" );
            ArgumentAssert.AssertArgumentRange( startIndex, 0, source.Length - 1, "startIndex" );

            //调用
            return AddToArray<T>( target, source, startIndex, source.Length - startIndex );
        }

        /// <summary>
        /// 将源数组指定位置起的指定个数的元素添加到目标数组
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="target">源数组</param>
        /// <param name="source">目标数组</param>
        /// <param name="startIndex">源数组的起始位置</param>
        /// <param name="count">拷贝元素的个数</param>
        /// <returns>合并之后包含源数组和目标数组内容的新数组</returns>
        /// <exception cref="System.ArgumentNullException"><paramref name="source"/>为Null</exception>
        /// <exception cref="System.IndexOutOfRangeException"><paramref name="startIndex"/>为-1或者超出target数组的范围</exception>
        /// <exception cref="System.IndexOutOfRangeException"><paramref name="count"/>为-1或者超出target数组的范围</exception>
        /// <remarks>如果源数组或者目标数组为空，那么将返回仅包含目标数组或者源数组内容的新数组</remarks>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-11 10:45    Created
        /// </history>
        public static T[] AddToArray<T>( T[] target, T[] source, int startIndex, int count )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( source, "source" );
            ArgumentAssert.AssertArgumentRange( startIndex, 0, source.Length - 1, "startIndex" );
            ArgumentAssert.AssertArgumentRange( count, 0, source.Length - startIndex, "count" );

            //计算总的长度
            int length = 0;

            //记录索引
            int index = 0;

            //添加
            if (null != target && target.Length > 0)
            {
                //计算长度
                length += target.Length;

                //计算索引
                index = target.Length;
            }

            //添加
            if (null != source && count > 0)
            {
                //计算长度
                length += count;
            }

            //创建临时数组
            T[] result = new T[length];

            //添加
            if (null != target && target.Length > 0)
            {
                target.CopyTo( result, 0 );
            }

            //添加
            if (null != source && count > 0)
            {
                Array.Copy( source, startIndex, result, index, count );
            }

            //返回
            return result;
        }

        /// <summary>
        /// 删除指定数组的指定索引位置的元素
        /// </summary>
        /// <typeparam name="T">数组类型参数</typeparam>
        /// <param name="source">处理的源数组</param>
        /// <param name="index">删除元素的索引位置</param>
        /// <returns>包含结果的新数组实例</returns>
        /// <remarks></remarks>
        /// <exception cref="System.ArgumentNullException"><paramref name="source"/>为Null</exception>
        /// <exception cref="System.IndexOutOfRangeException"><paramref name="index"/>为-1或者超出target数组的范围</exception>
        /// <history>
        /// [zanezeng]   2008-11-18 10:48    Created
        /// </history>
        public static T[] RemoveAt<T>( T[] source, int index )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( source, "source" );
            ArgumentAssert.AssertArgumentRange( index, 0, source.Length - 1, "index" );

            //建立列表
            List<T> list = new List<T>( source );

            //移除指定元素
            list.RemoveAt( index );

            //返回结果
            return list.ToArray();
        }

        /// <summary>
        /// 从指定的数组中删除指定的元素
        /// </summary>
        /// <typeparam name="T">数组类型参数</typeparam>
        /// <param name="source">处理的源数组</param>
        /// <param name="candidate">需要从源数组中删除的元素</param>
        /// <returns>包含结果的新数组实例</returns>
        /// <remarks></remarks>
        /// <exception cref="System.ArgumentNullException"><paramref name="source"/>为Null</exception>
        /// <history>
        /// [zanezeng]   2008-11-18 11:01    Created
        /// </history>
        public static T[] Remove<T>( T[] source, T candidate )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( source, "source" );

            //建立查询
            var query = from item in source
                        where !object.Equals( item, candidate )
                        select item;

            //返回
            return query.ToArray();
        }

        /// <summary>
        /// 从指定的数组中删除指定的元素
        /// </summary>
        /// <typeparam name="T">数组类型参数</typeparam>
        /// <param name="source">处理的源数组</param>
        /// <param name="candidate">需要从源数组中删除的元素</param>
        /// <param name="comparer">对象比较器</param>
        /// <returns>包含结果的新数组实例</returns>
        /// <remarks></remarks>
        /// <exception cref="System.ArgumentNullException"><paramref name="source"/>为Null</exception>
        /// <exception cref="System.ArgumentNullException"><paramref name="comparer"/>为Null</exception>
        /// <history>
        /// [zanezeng]   2008-11-18 11:01    Created
        /// </history>
        public static T[] Remove<T>( T[] source, T candidate, IEqualityComparer<T> comparer )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( source, "source" );
            ArgumentAssert.AssertArgumentNotNull( comparer, "comparer" );

            //建立查询
            var query = from item in source
                        where !comparer.Equals( item, candidate )
                        select item;

            //返回
            return query.ToArray();
        }

        #endregion

        #region ---Cast

        /// <summary>
        /// 将指定的源类型数组转化为目标类型数组
        /// </summary>
        /// <typeparam name="TSource">源数组类型</typeparam>
        /// <typeparam name="TTarget">目标数组类型</typeparam>
        /// <param name="source">源数组</param>
        /// <returns>转化后的目标数组</returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="source"/>为<c>null</c>引用。</exception>
        /// <history>
        /// [zanezeng]   2008-11-24 13:27    Created
        /// </history>
        public static TTarget[] DirectlyCase<TSource, TTarget>( TSource[] source )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( source, "source" );

            //用于保存结果
            TTarget[] result = new TTarget[source.Length];

            //循环转化
            for (int index = 0; index < source.Length; index++)
            {
                result[index] = (TTarget)(object)source[index];
            }

            //返回
            return result;
        }

        /// <summary>
        /// 使用AS操作符将源类型数组转换为目标类型数组
        /// </summary>
        /// <typeparam name="TSource">源数组类型</typeparam>
        /// <typeparam name="TTarget">目标数组类型</typeparam>
        /// <param name="source">源数组</param>
        /// <returns>转化后的目标数组。如果源类型的对象无法转换为目标类型的对象，则转换为<c>null</c>引用。</returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="source"/>为<c>null</c>引用。</exception>
        /// <history>
        /// [zanezeng]               2010/1/15 16:49    创建
        /// </history>
        public static TTarget[] SafeCase<TSource, TTarget>( TSource[] source )
            where TSource : class
            where TTarget : class
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( source, "source" );

            //用于保存结果
            TTarget[] result = new TTarget[source.Length];

            //循环转化
            for (int index = 0; index < source.Length; index++)
            {
                //转换为目标类型
                result[index] = source[index] as TTarget;
            }

            //返回
            return result;
        }

        /// <summary>
        /// 使用AS操作符将源类型数组转换为目标类型数组
        /// </summary>
        /// <typeparam name="TSource">源数组类型</typeparam>
        /// <typeparam name="TTarget">目标数组类型</typeparam>
        /// <param name="source">源数组</param>
        /// <param name="isTrimNull">是否去掉转换结果中的空引用</param>
        /// <returns>
        /// 转化后的目标数组。如果源类型的对象无法转换为目标类型的对象，则转换为<c>null</c>引用。
        /// </returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="source"/>为<c>null</c>引用。</exception>
        /// <history>
        /// [zanezeng]               2010/1/15 16:49    创建
        /// </history>
        public static TTarget[] SafeCase<TSource, TTarget>( TSource[] source, bool isTrimNull )
            where TSource : class
            where TTarget : class
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( source, "source" );

            //判断是否去掉空引用
            if (isTrimNull)
            {
                //用于保存结果
                List<TTarget> result = new List<TTarget>( source.Length );

                //用于保存临时转换的结果
                TTarget tempObj = null;

                //循环转化
                for (int index = 0; index < source.Length; index++)
                {
                    //转换为目标类型
                    tempObj = source[index] as TTarget;

                    //判断是否为空引用
                    if (null != tempObj)
                    {
                        //如果不是空引用，则添加到结果中
                        result.Add( tempObj );
                    }
                }

                //返回结果
                return result.ToArray();
            }
            else
            {
                //转调
                return SafeCase<TSource, TTarget>( source );
            }
        }

        /// <summary>
        /// 将指定的数组转化为字符串
        /// </summary>
        /// <typeparam name="T">数组类型</typeparam>
        /// <param name="source">需要转化的数组</param>
        /// <returns>转化之后的字符串</returns>
        /// <remarks>如果数组为Null，则返回空字符串</remarks>
        /// <history>
        /// [zanezeng]   2008-11-25 14:06    Created
        /// </history>
        public static string ArrayToSring<T>( T[] source )
        {
            //判断是否为Null
            if (null == source)
            {
                //返回空白字符串
                return string.Empty;
            }

            //创建字符串Builder
            StringBuilder builder = new StringBuilder();

            //添加
            builder.Append( typeof( T ).FullName );
            builder.Append( "[" );
            builder.Append( source.Length );
            builder.Append( "]" );
            builder.Append( "{" );

            //判断是否需要添加了逗号
            bool needComma = false;

            //循环添加字符串
            foreach (T item in source)
            {
                //添加逗号
                if (needComma)
                {
                    builder.Append( "," );
                }

                //添加元素
                builder.Append( "[" );
                builder.Append( item );
                builder.Append( "]" );

                //设置需要逗号
                needComma = true;
            }

            //添加结束大括号
            builder.Append( "}" );

            //返回
            return builder.ToString();
        }

        /// <summary>
        /// 将指定的数组转化为字符串
        /// </summary>
        /// <param name="source">需要转化的数组</param>
        /// <returns>转化之后的字符串</returns>
        /// <remarks>如果数组为Null，则返回空字符串</remarks>
        /// <history>
        /// [zanezeng]   2008-11-25 14:06    Created
        /// </history>
        public static string ArrayToString( object[] source )
        {
            //判断是否为Null
            if (null == source)
            {
                //返回空白字符串
                return string.Empty;
            }

            //创建字符串Builder
            StringBuilder builder = new StringBuilder();

            //添加
            builder.Append( typeof( object ).FullName );
            builder.Append( "[" );
            builder.Append( source.Length );
            builder.Append( "]" );
            builder.Append( "{" );

            //判断是否需要添加了逗号
            bool needComma = false;

            //循环添加字符串
            foreach (object item in source)
            {
                //添加逗号
                if (needComma)
                {
                    builder.Append( "," );
                }

                //添加元素
                builder.Append( "[" );
                builder.Append( item );
                builder.Append( "]" );

                //设置需要逗号
                needComma = true;
            }

            //添加结束大括号
            builder.Append( "}" );

            //返回
            return builder.ToString();
        }

        #endregion

        #region ---Dictionary

        /// <summary>
        /// 获得给定字典的给点键对应的值
        /// </summary>
        /// <typeparam name="TKey">字典键的类型参数</typeparam>
        /// <typeparam name="TValue">字典值的类型参数</typeparam>
        /// <param name="dictionary">字典</param>
        /// <param name="key">要获取的值的键</param>
        /// <param name="defaultValue">找不到给点键时，返回的默认值</param>
        /// <returns>如果找到该键，便会返回与指定的键相关联的值；否则，则会返回<paramref name="defaultValue"/></returns>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="dictionary"/>为Null</exception>
        /// <history>
        /// [ZaneZeng]               2009-5-7 9:23    创建
        /// </history>
        public static TValue GetDictionaryValue<TKey, TValue>( IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( dictionary, "dictionary" );

            //用于保存值
            TValue value;

            //尝试获得值
            if (dictionary.TryGetValue( key, out value ))
            {
                //如果找到键，则返回值
                return value;
            }

            //如果找不到键，则返回默认值
            return defaultValue;
        }

        #endregion
    }
}
